$(function() {

	//获取登陆用户session
	$.ajax({
		type: "POST",
		url: "http://127.0.0.1:8080/user/getSession",
		dataType: "json",
		xhrFields: {
			withCredentials: true
		},
		success: function(data) {
			var code = data.code;
			if (code == 'success') {
				var node = $("#login_but");
				node.empty();
				var html = '<li style="color:#ffffff">' + data.data.userName + '，欢迎回来</li>';
				html += '<li>|</li><li><a href="javascript:void(0)" id="logout">退出</a></li>';
				node.html(html);
			} else {
				window.location.href = './login.html';
			}
		}
	});

	//获取个人订单
	$.ajax({
		type: "POST",
		url: "http://127.0.0.1:8080/order/getMyOrder",
		dataType: "json",
		xhrFields: {
			withCredentials: true
		},
		success: function(data) {
			var arr = data.data;
			var html = "";
			var node = $("#order_list");
			arr.forEach(function(obj) {
				html += '<div class="order-status4"><div class="order-title">' +
					'<div class="dd-num">订单编号：' + obj.orderId + '</div></div>';
				var orderList = obj.goodsList;
				orderList.forEach(function(order) {
					console.log(order)
					var goods = order.goods;
					html += '<div class="order-content"><div class="order-left">' +
						'<ul class="item-list"><li class="td td-item"><div class="item-pic">' +
						'<img src="' + goods.imgUrl + '" class="itempic J_ItemImg">' +
						'</div><div class="item-info"><div class="item-basic-info">' +
						'<p>' + goods.goodsName + '</p><p class="info-little">' + goods.description +
						'</p></div></div></li><li class="td td-price"><div class="item-price">' + goods.price +
						'元</div></li><li class="td td-number"><div class="item-number">' + order.grade +
						'分</div></li><li class="td td-operation"><div class="item-operation">' + order.comment +
						'</div></li><li class="td td-operation">' +
						'<a href="javascript:void(0)" id="judge" data-id="' + order.id +
						'"><div class="am-btn am-btn-warning anniu">' +
						'评价商品</div></a><a href="javascript:void(0)" id="deleteGoods" data-id="' + order.id +
						'"><div class="am-btn am-btn-danger anniu">' +
						'删除商品</div></a></li></ul></div></div></div>';
				})
			})
			node.html(html);
		}
	});


	//删除订单商品
	$("#order_list").on("click", "#deleteGoods", function() {
		var id = $(this).attr("data-id");
		$.ajax({
			type: "POST",
			url: "http://127.0.0.1:8080/order/deleteItemGrade",
			dataType: "json",
			data: {
				"itemId": id
			},
			success: function(data) {
				var code = data.code;
				if ("success" == code) {
					layer.msg("删除成功");
					window.location.reload();
				} else {
					layer.msg("删除失败");
				}
			}
		});
	})

	//点击评价商品按钮
	$("#order_list").on("click", "#judge", function() {
		var id = $(this).attr("data-id");
		var html = '<div style="padding:15px 20px;">' +
			'<div><input type="radio" value="1" name="goods_grade" id="goods_grade1"><label for="goods_grade1">&nbsp;1分</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' +
			'<input type="radio" value="3" name="goods_grade" id="goods_grade2"><label for="goods_grade2">&nbsp;2分</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' +
			'<input type="radio" value="3" name="goods_grade" id="goods_grade3"><label for="goods_grade3">&nbsp;3分</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' +
			'<input type="radio" value="4" name="goods_grade" id="goods_grade4"><label for="goods_grade4">&nbsp;4分</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' +
			'<input type="radio" value="5" name="goods_grade" id="goods_grade5"><label for="goods_grade5">&nbsp;5分</label></div>' +
			'<hr/><div><span>请输入评价内容：</span><br/><textarea id="comment_context" rows="6" cols="48"></textarea></div></div>';
		layer.open({
			title: "商品评价",
			type: 1,
			skin: 'layui-layer-rim',
			area: ['420px', '300px'],
			btn: ['确定', '关闭'],
			yes: function(index) {
				var grade = $("input[name='goods_grade']:checked").val();
				var comment = $("#comment_context").val();
				if (grade == '' || grade == undefined) {
					layer.msg("请选择评分");
					return;
				}
				if (comment == '') {
					layer.msg("请输入评论内容");
					return;
				}
				$.ajax({
					type: "POST",
					url: "http://127.0.0.1:8080/order/updateItemGrade",
					dataType: "json",
					data: {
						"itemId": id,
						"grade": grade,
						"comment": comment
					},
					success: function(data) {
						layer.msg(data.msg);
						layer.close(index);
					}
				});
			},
			btn2: function(index) {
				layer.close(index);
			},
			content: html
		});
	})
})
